import {rmEmptyDirs} from '../utils/rmemptydirs';
const BasicModel = require('./BasicModel');
const fs = require('fs');
const path = require('path');

module.exports = class extends BasicModel {
  async getFilesListByIdAction(params) {
    const listall = this.model(params.model).where({relation_id: params.data.relation_id}).select();
    return listall;
  }
  async uploadfilesAction(params) {
    const data = this.model(params.model).add(params.filedata);
    return data;
  }

  async delAction(params) {
    const filesinfo = await this.model('recruit_enclosure').where({id: params.id}).find();
    if (think.isEmpty(filesinfo)) {
      return '文件不存在';
    }
    await fs.unlink(filesinfo.file_path, (err) => {
      if (err) {
        return err;
      }
      const dirpath = path.dirname(filesinfo.file_path);
      // 删除空文件夹
      rmEmptyDirs(dirpath);
    });
    const data = await this.model('recruit_enclosure').where({id: params.id}).delete();
    return data;
  }

  // listallAction(params) {
  //   let data;
  //   var arr = Object.keys(params.data);// 传空返回全部数据，不为空选择查询
  //   if (arr.length !== 0) {
  //     data = this.order('project_start_time').where({...params.data}).select();
  //     return data;
  //   } else {
  //     data = this.order('project_start_time').select();
  //     return data;
  //   }
  // }
};
